namespace PureRobot.Digi.ConnectME.JumpStart.SwitchedLight
{
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading;
    using Microsoft.SPOT;
    using Microsoft.SPOT.Hardware;

    /// <summary>
    /// This class sets up the 4th and 5th IO pins as switches that control the operation of an LED attached to IO pin 1.
    /// </summary>
    /// <remarks>
    /// This code was used whilst testing an LED hooked up to the IO pin with a BC547 transistor. 
    /// Having a solid on LED was useful whilst testing the voltages and current passing through the circuit and the blinking modes gave the circuit a little workout to make sure nothing was overheating or breaking down when operated for a day or so continuously.
    /// </remarks>
    public class Start
    {
        private enum LightMode
        {
            SolidOn,
            BlinkSlow,
            BlinkFast,
            SliderBlink,
        }

        public static void Main()
        {
            int sliderBlinkSpeed = 1;
            bool sliderBounce = false;
            using (InputPort switch4 = new InputPort(RealGpioPins.Gpio4, false, Port.ResistorMode.PullDown))
            {
                using (InputPort switch5 = new InputPort(RealGpioPins.Gpio5, false, Port.ResistorMode.PullDown))
                {
                    using (OutputPort led1 = new OutputPort(RealGpioPins.Gpio1, false))
                    {
                        while (true)
                        {
                            bool switch4State = switch4.Read();
                            bool switch5State = switch5.Read();

                            LightMode lightMode;
                            if (!switch5State && !switch4State)
                            {
                                lightMode = LightMode.SolidOn;
                            }
                            else if (switch5State && switch4State)
                            {
                                lightMode = LightMode.BlinkSlow;
                            }
                            else if (!switch5State && switch4State)
                            {
                                lightMode = LightMode.BlinkFast;
                            }
                            else
                            {
                                lightMode = LightMode.SliderBlink;
                            }

                            switch (lightMode)
                            {
                                case LightMode.SolidOn:
                                    {
                                        led1.Write(true);
                                        Thread.Sleep(100);
                                        break;
                                    }
                                case LightMode.BlinkSlow:
                                    {
                                        led1.Write(!led1.Read());
                                        Thread.Sleep(250);
                                        break;
                                    }
                                case LightMode.BlinkFast:
                                    {
                                        led1.Write(!led1.Read());
                                        Thread.Sleep(50);
                                        break;
                                    }
                                case LightMode.SliderBlink:
                                    {
                                        led1.Write(!led1.Read());
                                        Thread.Sleep(5 * sliderBlinkSpeed);
                                        if (sliderBlinkSpeed > 40)
                                        {
                                            sliderBounce = true;
                                        }
                                        else if (sliderBlinkSpeed <= 1)
                                        {
                                            sliderBounce = false;
                                        }
                                        if (sliderBounce)
                                        {
                                            sliderBlinkSpeed--;
                                        }
                                        else
                                        {
                                            sliderBlinkSpeed++;
                                        }
                                        break;
                                    }
                            }

                        }
                    }
                }
            }
        }
    }
}
